// Copyright 2011 Google Inc. All Rights Reserved.
// Author: jacobsa@google.com (Aaron Jacobs)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var registerAction = flame.registerAction;

//////////////////////////////////////////////////////
// Helpers
//////////////////////////////////////////////////////

function RegisterActionTest() {}
registerTestSuite(RegisterActionTest);

RegisterActionTest.prototype.tearDown = function() {
  // Replace the global state we modified.
  flame.internal.registeredActions = new flame.internal.StringMap;
  flame.internal.outputFileIndex = new flame.internal.StringMap;
};

//////////////////////////////////////////////////////
// Tests
//////////////////////////////////////////////////////

RegisterActionTest.prototype.OneAction = function() {
  var action = new flame.Action('foo/bar:baz');
  action.outputs = ['taco', 'burrito'];
  registerAction(action);

  // Action names
  expectThat(flame.internal.registeredActions.getKeys(),
             whenSorted(elementsAre(['foo/bar:baz'])));

  expectEq(action, flame.internal.registeredActions.get('foo/bar:baz'));

  // Outputs
  expectThat(flame.internal.outputFileIndex.getKeys(),
             whenSorted(elementsAre(['burrito', 'taco'])));

  expectEq(action, flame.internal.outputFileIndex.get('burrito'));
  expectEq(action, flame.internal.outputFileIndex.get('taco'));
};

RegisterActionTest.prototype.TwoIdenticallyNamedActions = function() {
  var action1 = new flame.Action('foo/bar:baz');
  var action2 = new flame.Action('foo/bar:baz');

  registerAction(action1);

  expectThat(function() { registerAction(action2); },
             throwsError(/foo\/bar:baz.*already registered/));
};

RegisterActionTest.prototype.ActionsWithOverlappingOutputs = function() {
  var action1 = new flame.Action('foo/bar:baz');
  var action2 = new flame.Action('qux:taco');

  action1.outputs = ['taco', 'burrito'];
  action2.outputs = ['enchilada', 'taco'];

  registerAction(action1);

  expectThat(function() { registerAction(action2) },
             throwsError(/taco.*foo\/bar:baz.*qux:taco/));
};

RegisterActionTest.prototype.TwoDifferentActions = function() {
  var action1 = new flame.Action('foo/bar:baz');
  var action2 = new flame.Action('qux:taco');

  action1.outputs = ['taco', 'burrito'];
  action2.outputs = ['enchilada'];

  registerAction(action1);
  registerAction(action2);

  // Actions
  expectThat(flame.internal.registeredActions.getKeys(),
             whenSorted(elementsAre([
                 'foo/bar:baz',
                 'qux:taco',
             ])));

  expectEq(action1, flame.internal.registeredActions.get('foo/bar:baz'));
  expectEq(action2, flame.internal.registeredActions.get('qux:taco'));

  // Outputs
  expectThat(flame.internal.outputFileIndex.getKeys(),
             whenSorted(elementsAre(['burrito', 'enchilada', 'taco'])));

  expectEq(action1, flame.internal.outputFileIndex.get('burrito'));
  expectEq(action1, flame.internal.outputFileIndex.get('taco'));
  expectEq(action2, flame.internal.outputFileIndex.get('enchilada'));
};
